const tabs = {
    namespace: 'tabs',
    state: {
      tabs: [],
      activeKey: null
    },
    reducers: {
      add(state, { payload: tab }) {
        const _tab = state.tabs.find(item => item.key === tab.key)
        if (_tab) return {...state, activeKey:tab.key}
        return { activeKey:tab.key,tabs: [...state.tabs, tab] }
      },
      remove(state, { payload: key }) {
        if (state.activeKey === key) {
          const index = state.tabs.findIndex(tab => tab.key === key)
          const before = state.tabs[index - 1]
          const after = state.tabs[index + 1]
          const activeKey = before?before.key:after? after.key: null
          return { activeKey, tabs: state.tabs.filter(tab => tab.key !== key)}
        }
        return { ...state, tabs: state.tabs.filter(tab => tab.key !== key) }
      },
      change(state, { payload: activeKey }) {
        return { ...state, activeKey }
      }
    },
    effects: {}
  }
  export default tabs
  